import axios from "axios";
import { Loading, Notification } from "element-ui";
// const qs = require("qs");

let base = config.ip;

console.log(base);

axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded";

let loading;
//内存中正在请求的数量
let loadingNum = 0;
function startLoading() {
  if (loadingNum == 0) {
    loading = Loading.service({
      lock: true,
      text: "拼命加载中...",
      background: "rgba(255,255,255,0.5)"
    });
  }
  //请求数量加1
  loadingNum++;
}
function endLoading() {
  //请求数量减1
  loadingNum--;
  if (loadingNum <= 0) {
    loading.close();
  }
}

//请求数据拦截器
axios.interceptors.request.use(
  request => {
    startLoading();
    return request;
  },
  err => {
    return Promise.reject(err);
  }
);

//接收响应拦截器
axios.interceptors.response.use(
  response => {
    endLoading();
    if (response.headers["content-type"] == "application/force-download") {
      if (response.status != 200) {
        Notification.error({
          title: "错误",
          message: response.data.message
        });
      }
    } else {
      if (response.data.status != 200) {
        Notification.error({
          title: "错误",
          message: response.data.message
        });
      }
    }

    return response;
  },
  err => {
    endLoading();
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = "请求错误(400)";
          break;
        // case 401: this.$router.push('/login'); break;
        case 403:
          err.message = "拒绝访问(403)";
          break;
        case 404:
          err.message = "请求出错(404)";
          break;
        case 408:
          err.message = "请求超时(408)";
          break;
        case 500:
          err.message = "服务器错误(500)";
          break;
        case 501:
          err.message = "服务未实现(501)";
          break;
        case 502:
          err.message = "网络错误(502)";
          break;
        case 503:
          err.message = "服务不可用(503)";
          break;
        case 504:
          err.message = "网络超时(504)";
          break;
        case 505:
          err.message = "HTTP版本不受支持(505)";
          break;
        default:
          err.message = `连接出错(${err.response.status})!`;
      }
    } else {
      err.message = "连接服务器失败!";
    }
    Notification.error({
      title: "错误",
      message: "请求失败，稍后重试。"
    });
    return Promise.reject(err);
  }
);

// export const requestLogin = params => {
//   return axios.get(`/static/user.json`, params).then(res => {
//     return {
//       user: res.data,
//       msg: "登陆成功",
//       code: 200
//     };
//   });
// };

// export const getEnterprise = () => {
//   return axios
//     .post(`${base}/internet-plus-ontology-library/ServicePortal/getEnterprise`)
//     .then(res => {
//       return res.data;
//     });
// };

// /* 获取自证合规信息公开总入口 */
// export const getInfoDisclosure = tid => {
//   return axios
//     .post(
//       `${base}/internet-plus-ontology-library/ServicePortal/getInfoDisclosure`,
//       qs.stringify({
//         tid: tid
//       })
//     )
//     .then(res => {
//       return res.data;
//     });
// };

// /* 获取自证合规公开详细信息 */
// export const getEssentialLog = (tid, pageNum, pageSize) => {
//   return axios
//     .get(
//       `${base}/internet-plus-regulation-calculate/operate/log?code=${tid}&pageNum=${pageNum}&pageSize=${pageSize}`
//     )
//     .then(res => {
//       return res.data;
//     });
// };
